/*
 * Copyright (C) 2022 Polygon Zone Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http:// www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */






#if 0
#include "music_play.h"
#include "beep.h"
#include "decode.h"


Play_Ctrl_t g_play_ctrl;
Song_Ctrl_t g_song_ctrl;
TaskHandle_t Music_Handle;
static portMUX_TYPE music_spinlock = portMUX_INITIALIZER_UNLOCKED;



/* 歌曲 */
const struct beep_song music_song[5] =
{
	{
    	.name = "Two_Tigers",
    	.data = {
        	0x15, 0x02, 0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x15, 0x02,
        	0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x17, 0x02, 0x18, 0x02,
        	0x19, 0x01, 0x17, 0x02, 0x18, 0x02, 0x19, 0x01, 0x19, 0x03,
        	0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02, 0x15, 0x16,
        	0x19, 0x03, 0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02,
        	0x15, 0x16, 0x15, 0x02, 0x0F, 0x02, 0x15, 0x01, 0x15, 0x02,
        	0x0F, 0x02, 0x15, 0x01, 0x00, 0x00
    	}
	},
    {
		.name = "chongerfei",
		.data ={
			0x17,0x02, 0x17,0x03, 0x17,0x03, 0x18,0x02, 0x19,0x02,
                                  0x17,0x66, 0x16,0x03, 0x16,0x01, 0x15,0x02, 0x15,0x03,
                                  0x15,0x03, 0x16,0x02, 0x17,0x02, 0x17,0x66, 0x11,0x03,
                                  0x11,0x01, 0x10,0x02, 0x17,0x02, 0x16,0x01, 0x10,0x02,
                                  0x17,0x02, 0x16,0x01, 0x10,0x02, 0x17,0x02, 0x16,0x66,
                                  0x15,0x03, 0x15,0x14, 0x17,0x03, 0x16,0x03, 0x19,0x00,
                                  0x18,0x03, 0x17,0x03, 0x17,0x03, 0x16,0x03, 0x16,0x01,
                                  0x19,0x03, 0x18,0x03, 0x17,0x02, 0x18,0x03, 0x19,0x66,
                                  0x17,0x02, 0x16,0x14, 0x15,0x03, 0x10,0x02, 0x17,0x02,
                                  0x16,0x66, 0x15,0x03, 0x0F,0x02, 0x16,0x03, 0x15,0x03,
                                  0x15,0x01, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
                                  0x15,0x01, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
                                  0x15,0x66, 0x16,0x03, 0x15,0x14, 0x00,0x00 
	    }
	},
	{
		.name = "huanlesong",
		.data ={
			0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x02, 0x19,0x02,
                                  0x18,0x02, 0x17,0x02, 0x16,0x02, 0x15,0x02, 0x15,0x02,
                                  0x16,0x02, 0x17,0x02, 0x17,0x66, 0x16,0x03, 0x16,0x01,
                                  0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x02, 0x19,0x02,
                                  0x18,0x02, 0x17,0x02, 0x16,0x02, 0x15,0x02, 0x15,0x02,
                                  0x16,0x02, 0x17,0x02, 0x16,0x66, 0x15,0x03, 0x15,0x01,
                                  0x16,0x02, 0x16,0x02, 0x17,0x02, 0x15,0x02, 0x16,0x02,
                                  0x17,0x03, 0x18,0x03, 0x17,0x02, 0x15,0x02, 0x16,0x02,
                                  0x17,0x03, 0x18,0x03, 0x17,0x02, 0x16,0x02, 0x15,0x02,
                                  0x16,0x02, 0x0F,0x02, 0x17,0x02, 0x17,0x02, 0x17,0x02,
                                  0x18,0x02, 0x19,0x02, 0x19,0x02, 0x18,0x02, 0x17,0x02,
                                  0x18,0x03, 0x16,0x03, 0x15,0x02, 0x15,0x02, 0x16,0x02,
                                  0x17,0x02, 0x16,0x66, 0x15,0x03, 0x15,0x01, 0x00,0x00 
	    }
		
	},
	{
		.name = "blink blink",
		.data ={
			0x15,0x02, 0x15,0x02, 0x19,0x02, 0x19,0x02, 0x1A,0x02,
            0x1A,0x02, 0x19,0x01, 0x18,0x02, 0x18,0x02, 0x17,0x02,
            0x17,0x02, 0x16,0x02, 0x16,0x02, 0x15,0x01, 0x19,0x02,
            0x19,0x02, 0x18,0x02, 0x18,0x02, 0x17,0x02, 0x17,0x02,
            0x16,0x01, 0x19,0x02, 0x19,0x02, 0x18,0x02, 0x18,0x02,
            0x17,0x02, 0x17,0x02, 0x16,0x01, 0x15,0x02, 0x15,0x02,
            0x19,0x02, 0x19,0x02, 0x1A,0x02, 0x1A,0x02, 0x19,0x01,
            0x18,0x02, 0x18,0x02, 0x17,0x02, 0x17,0x02, 0x16,0x02,
            0x16,0x02, 0x15,0x01, 0x00,0x00
	    }
		
	},
	
	{
		.name = "xiaomifeng",
		.data ={
			0x19,0x03, 0x19,0x03, 0x17,0x02, 0x18,0x03, 0x16,0x03,
			0x16,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
			0x19,0x03, 0x19,0x03, 0x19,0x02, 0x19,0x03, 0x17,0x03,
			0x17,0x02, 0x18,0x03, 0x16,0x03, 0x16,0x02, 0x15,0x03,
			0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x01, 0x16,0x03,
			0x16,0x03, 0x16,0x03, 0x16,0x03, 0x16,0x03, 0x17,0x03,
			0x18,0x02, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03,
			0x17,0x03, 0x18,0x03, 0x19,0x02, 0x19,0x03, 0x17,0x03,
			0x17,0x02, 0x18,0x03, 0x16,0x03, 0x16,0x02, 0x15,0x03,
			0x17,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x01, 0x00,0x00
			
	    }
		
	},
		
};









void Music_Play_Task(void *arg){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	beep_song_decode_init();
	while(1){
		/* 发送消息 */
		if(play_ctrl->play_status == PLAYER_RUNNING){
			switch(play_ctrl->ctrl_status){
				case(PLAYER_INIT):{
					/* 初始化歌曲 */
					song_ctrl->song = &music_song[play_ctrl->song_current];
					song_ctrl->cnt = 0;
					song_ctrl->music_len = beep_song_get_len(song_ctrl->song);
					play_ctrl->ctrl_status = PLAYER_SONG;
					play_ctrl->play_finsh = 0;
					break;
				}
				case(PLAYER_SONG):{
					/* 播放歌曲 */
					if(song_ctrl->cnt<song_ctrl->music_len){
						beep_song_get_data(song_ctrl->song, song_ctrl->cnt,&(song_ctrl->data));
						beep_set(song_ctrl->data.freq, LEDC_BUZZ_MAX_DUTY);
						//printf("music freq = %d\r\n",song_ctrl->data.freq);
						vTaskDelay(song_ctrl->data.sound_len/portTICK_PERIOD_MS);
						beep_off();
						vTaskDelay(song_ctrl->data.nosound_len/portTICK_PERIOD_MS);
						song_ctrl->cnt++;
					}else{
						/* 播放完成 */
						play_ctrl->play_status = PLAYER_STOP;
						play_ctrl->ctrl_status = PLAYER_INIT;
						song_ctrl->cnt = 0;
						song_ctrl->music_len = 0;
						play_ctrl->play_finsh = 1;
						beep_off();
					}
					break;
				}
			}
		}else if(play_ctrl->play_status == PLAYER_STOP){
			play_ctrl->ctrl_status = PLAYER_INIT;
			play_ctrl->play_finsh = 1;
			//beep_off();
			vTaskDelay(10);
		}
	}
}




/* MP3 初始化任务 */
int Music_Init(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	//Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	/* 创建  音乐任务 */
	play_ctrl->play_status = PLAYER_STOP;
	play_ctrl->ctrl_status = PLAYER_INIT;
	if(play_ctrl->task_is_create == 0){
		xTaskCreatePinnedToCore(Music_Play_Task,"music task",2048,NULL,15,&Music_Handle,1);
		play_ctrl->task_is_create = 1;
	}
	return 0;
}




int music_select(int selc){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	if(selc>5 || selc<1){
		return -1;
	}
	/* 进入临界区 */
	portENTER_CRITICAL(&music_spinlock);
	play_ctrl->ctrl_status = PLAYER_RUNNING;
	play_ctrl->play_status = PLAYER_INIT;
	play_ctrl->song_current=selc-1;
	play_ctrl->play_finsh = 0;
	song_ctrl->song = &music_song[play_ctrl->song_current];
	song_ctrl->cnt = 0;
	song_ctrl->music_len = beep_song_get_len(song_ctrl->song);
	/* 退出临界区 */
	portEXIT_CRITICAL(&music_spinlock);
	return 0;
}



int music_is_finsh(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	int tmp = 0;
	portENTER_CRITICAL(&music_spinlock);
	tmp = play_ctrl->play_finsh;
	portEXIT_CRITICAL(&music_spinlock);
	return tmp;
}





void music_stop(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	/* 进入临界区 */
	portENTER_CRITICAL(&music_spinlock);
	play_ctrl->play_status = PLAYER_STOP;
	play_ctrl->ctrl_status = PLAYER_INIT;
	song_ctrl->cnt = 0;
	song_ctrl->music_len = 0;
	play_ctrl->play_finsh = 1;
	/* 退出临界区 */
	portEXIT_CRITICAL(&music_spinlock);
	beep_off();
}
#endif 
#include "music_play.h"
#include "beep.h"
#include "decode.h"


Play_Ctrl_t g_play_ctrl;
Song_Ctrl_t g_song_ctrl;
TaskHandle_t Music_Handle;
static portMUX_TYPE music_spinlock = portMUX_INITIALIZER_UNLOCKED;



/* 歌曲 */
const struct beep_song music_song[5] =
{
	{
    	.name = "Two_Tigers",
    	.data = {
        	0x15, 0x02, 0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x15, 0x02,
        	0x16, 0x02, 0x17, 0x02, 0x15, 0x02, 0x17, 0x02, 0x18, 0x02,
        	0x19, 0x01, 0x17, 0x02, 0x18, 0x02, 0x19, 0x01, 0x19, 0x03,
        	0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02, 0x15, 0x16,
        	0x19, 0x03, 0x1A, 0x03, 0x19, 0x03, 0x18, 0x03, 0x17, 0x02,
        	0x15, 0x16, 0x15, 0x02, 0x0F, 0x02, 0x15, 0x01, 0x15, 0x02,
        	0x0F, 0x02, 0x15, 0x01, 0x00, 0x00
    	}
	},
    {
		.name = "chongerfei",
		.data ={
			0x17,0x02, 0x17,0x03, 0x17,0x03, 0x18,0x02, 0x19,0x02,
			0x17,0x66, 0x16,0x03, 0x16,0x01, 0x15,0x02, 0x15,0x03,
			0x15,0x03, 0x16,0x02, 0x17,0x02, 0x17,0x66, 0x11,0x03,
			0x11,0x01, 0x10,0x02, 0x17,0x02, 0x16,0x01, 0x10,0x02,
			0x17,0x02, 0x16,0x01, 0x10,0x02, 0x17,0x02, 0x16,0x66,
			0x15,0x03, 0x15,0x14, 0x17,0x03, 0x16,0x03, 0x19,0x00,
			0x18,0x03, 0x17,0x03, 0x17,0x03, 0x16,0x03, 0x16,0x01,
			0x19,0x03, 0x18,0x03, 0x17,0x02, 0x18,0x03, 0x19,0x66,
			0x17,0x02, 0x16,0x14, 0x15,0x03, 0x10,0x02, 0x17,0x02,
			0x16,0x66, 0x15,0x03, 0x0F,0x02, 0x16,0x03, 0x15,0x03,
			0x15,0x01, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
			0x15,0x01, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
			0x15,0x66, 0x16,0x03, 0x15,0x14, 0x00,0x00 
	    }
	},
	{
		.name = "huanlesong",
		.data ={
			0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x02, 0x19,0x02,
			0x18,0x02, 0x17,0x02, 0x16,0x02, 0x15,0x02, 0x15,0x02,
			0x16,0x02, 0x17,0x02, 0x17,0x66, 0x16,0x03, 0x16,0x01,
			0x17,0x02, 0x17,0x02, 0x18,0x02, 0x19,0x02, 0x19,0x02,
			0x18,0x02, 0x17,0x02, 0x16,0x02, 0x15,0x02, 0x15,0x02,
			0x16,0x02, 0x17,0x02, 0x16,0x66, 0x15,0x03, 0x15,0x01,
			0x16,0x02, 0x16,0x02, 0x17,0x02, 0x15,0x02, 0x16,0x02,
			0x17,0x03, 0x18,0x03, 0x17,0x02, 0x15,0x02, 0x16,0x02,
			0x17,0x03, 0x18,0x03, 0x17,0x02, 0x16,0x02, 0x15,0x02,
			0x16,0x02, 0x0F,0x02, 0x17,0x02, 0x17,0x02, 0x17,0x02,
			0x18,0x02, 0x19,0x02, 0x19,0x02, 0x18,0x02, 0x17,0x02,
			0x18,0x03, 0x16,0x03, 0x15,0x02, 0x15,0x02, 0x16,0x02,
			0x17,0x02, 0x16,0x66, 0x15,0x03, 0x15,0x01, 0x00,0x00 
	    }
		
	},
	{
		.name = "blink blink",
		.data ={
			0x15,0x02, 0x15,0x02, 0x19,0x02, 0x19,0x02, 0x1A,0x02,
            0x1A,0x02, 0x19,0x01, 0x18,0x02, 0x18,0x02, 0x17,0x02,
            0x17,0x02, 0x16,0x02, 0x16,0x02, 0x15,0x01, 0x19,0x02,
            0x19,0x02, 0x18,0x02, 0x18,0x02, 0x17,0x02, 0x17,0x02,
            0x16,0x01, 0x19,0x02, 0x19,0x02, 0x18,0x02, 0x18,0x02,
            0x17,0x02, 0x17,0x02, 0x16,0x01, 0x15,0x02, 0x15,0x02,
            0x19,0x02, 0x19,0x02, 0x1A,0x02, 0x1A,0x02, 0x19,0x01,
            0x18,0x02, 0x18,0x02, 0x17,0x02, 0x17,0x02, 0x16,0x02,
            0x16,0x02, 0x15,0x01, 0x00,0x00
	    }
		
	},
	{
		.name = "xiaomifeng",
		.data ={
			0x19,0x03, 0x19,0x03, 0x17,0x02, 0x18,0x03, 0x16,0x03,
			0x16,0x02, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
			0x19,0x03, 0x19,0x03, 0x19,0x02, 0x19,0x03, 0x17,0x03,
			0x17,0x02, 0x18,0x03, 0x16,0x03, 0x16,0x02, 0x15,0x03,
			0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x01, 0x16,0x03,
			0x16,0x03, 0x16,0x03, 0x16,0x03, 0x16,0x03, 0x17,0x03,
			0x18,0x02, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03,
			0x17,0x03, 0x18,0x03, 0x19,0x02, 0x19,0x03, 0x17,0x03,
			0x17,0x02, 0x18,0x03, 0x16,0x03, 0x16,0x02, 0x15,0x03,
			0x17,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x01, 0x00,0x00
			
	    }
		
	},
		
};









void Music_Play_Task(void *arg){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	beep_song_decode_init();
	while(1){
		/* 发送消息 */
		if(play_ctrl->play_status == PLAYER_RUNNING){
			switch(play_ctrl->ctrl_status){
				case(PLAYER_INIT):{
					/* 初始化歌曲 */
					song_ctrl->song = &music_song[play_ctrl->song_current];
					//song_ctrl->cnt = 0;
					song_ctrl->music_len = beep_song_get_len(song_ctrl->song);
					play_ctrl->ctrl_status = PLAYER_SONG;
					play_ctrl->play_finsh = 0;
					break;
				}
				case(PLAYER_SONG):{
					/* 播放歌曲 */
					if(song_ctrl->cnt<song_ctrl->music_len){
						beep_song_get_data(song_ctrl->song, song_ctrl->cnt,&(song_ctrl->data));
						beep_set(song_ctrl->data.freq, LEDC_BUZZ_MAX_DUTY);
						//printf("music freq = %d\r\n",song_ctrl->data.freq);
						vTaskDelay(song_ctrl->data.sound_len/portTICK_PERIOD_MS);
						beep_off();
						vTaskDelay(song_ctrl->data.nosound_len/portTICK_PERIOD_MS);
						song_ctrl->cnt++;
					}else{
						/* 播放完成 */
						play_ctrl->play_status = PLAYER_STOP;
						play_ctrl->ctrl_status = PLAYER_INIT;
						song_ctrl->cnt = 0;
						song_ctrl->music_len = 0;
						play_ctrl->play_finsh = 1;
						beep_off();
					}
					break;
				}
			}
		}else if(play_ctrl->play_status == PLAYER_STOP){
			play_ctrl->ctrl_status = PLAYER_INIT;
			play_ctrl->play_finsh = 1;
			//beep_off();
			vTaskDelay(10);
		}
	}
}




/* MP3 初始化任务 */
int Music_Init(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	//Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	/* 创建  音乐任务 */
	play_ctrl->play_status = PLAYER_STOP;
	play_ctrl->ctrl_status = PLAYER_INIT;
	if(play_ctrl->task_is_create == 0){
		xTaskCreatePinnedToCore(Music_Play_Task,"music task",2048,NULL,1,&Music_Handle,1);
		play_ctrl->task_is_create = 1;
	}
	return 0;
}




int music_select(int track,int progress){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	if(track>5 || track<1){
		return -1;
	}
	/* 进入临界区 */
	portENTER_CRITICAL(&music_spinlock);
	play_ctrl->ctrl_status = PLAYER_RUNNING;
	play_ctrl->play_status = PLAYER_INIT;
	play_ctrl->song_current=track-1;
	play_ctrl->play_finsh = 0;
	song_ctrl->song = &music_song[play_ctrl->song_current];
	song_ctrl->cnt = progress;
	song_ctrl->music_len = beep_song_get_len(song_ctrl->song);
	/* 退出临界区 */
	portEXIT_CRITICAL(&music_spinlock);
	return 0;
}





int music_get_progres(int *track, int *progress){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	if((play_ctrl->play_status == PLAYER_STOP) || (play_ctrl->play_finsh == 1)){
		return -1;
	}else{
		if((track!=NULL) && (progress!=NULL)){
			portENTER_CRITICAL(&music_spinlock);
			*progress = song_ctrl->cnt;
			*track    = play_ctrl->song_current +1 ;
			portEXIT_CRITICAL(&music_spinlock);
		}
		return 0;
	}
	
}




int music_is_finsh(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	int tmp = 0;
	portENTER_CRITICAL(&music_spinlock);
	tmp = play_ctrl->play_finsh;
	portEXIT_CRITICAL(&music_spinlock);
	return tmp;
}





void music_stop(void){
	Play_Ctrl_t *play_ctrl = &g_play_ctrl;
	Song_Ctrl_t *song_ctrl = &g_song_ctrl;
	/* 进入临界区 */
	portENTER_CRITICAL(&music_spinlock);
	play_ctrl->play_status = PLAYER_STOP;
	play_ctrl->ctrl_status = PLAYER_INIT;
	song_ctrl->cnt = 0;
	song_ctrl->music_len = 0;
	play_ctrl->play_finsh = 1;
	/* 退出临界区 */
	portEXIT_CRITICAL(&music_spinlock);
	beep_off();
}

























